package com.free.coder.log;

import java.io.*;
import java.util.Properties;

import com.yx.xf.core.util.Constant;
  

// Referenced classes of package com.suntek.eap.core.config:
//            SystemConfig

public class FileConfig
{

    private static Properties configPro = null;

    public FileConfig()
    {
    }

    
    
    
    
    
    
    private static Properties loadProperties()
    {
    	System.out.println("begin loadProperties");
    	 try{
    		 Properties fpro = new Properties();
    		 String configDir = Constant.getTomcatHome();
       
	        if(!configDir.endsWith("/") && !configDir.endsWith("\\"))
	        {
	            configDir = ( String.valueOf(configDir))+System.getProperty("file.separator") ;
	        }
	        configDir = ( String.valueOf(configDir))+"eapserver"+System.getProperty("file.separator")+"config" ;
	        File dir = new File(configDir);
	        if(!dir.isDirectory())
	        {
	            return fpro;
	        }
	        File files[] = dir.listFiles();
	        if(files == null)
	        {
	            return fpro;
	        }
	        for(int i = 0; i < files.length; i++)
	        {
	            if(files[i].getName().endsWith("properties"))
	            {
	                try
	                {
	                    fpro.load(new FileInputStream(files[i]));
	                }
	                catch(Exception ex)
	                {
	                	ex.printStackTrace();
	                   // BaseCoreLogger.logger.info((new StringBuilder("load config file[")).append(files[i].getName()).append("] errro! ").toString());
	                }
	            }
	        }
	        return fpro;
        }catch(Exception ex){
        	System.out.println("load property error");
        }
        return  new Properties();
    }

    public static void loadProperties(File configFile)
    {
        if(configFile == null)
        {
            return;
        }
        
        if(configPro==null)loadProperties();
        synchronized(configPro)
        {
            try
            {
                configPro.load(new FileInputStream(configFile));
            }
            catch(Exception ex)
            {
            	ex.printStackTrace();
                //BaseCoreLogger.logger.info((new StringBuilder("load config file[")).append(configFile).append("] errro! ").toString());
            }
        }
    }

    public static String getProperty(String key)
    {
        if(key == null)
        {
            return null;
        } else
        {
            return configPro.getProperty(key);
        }
    }

    public static String getProperty(String key, String defaultValue)
    {
        String ret = null;
        try
        {
            ret = getProperty(key);
        }
        catch(Exception e)
        {
            return defaultValue;
        }
        if(ret == null)
        {
            return defaultValue;
        } else
        {
            return ret;
        }
    }

}
